﻿using Basic.FormRemoteModel;
using Basic.FormRemoteModel.PhyFormInter.Model;
using Basic.OACollect;
using Basic.OAService.Interface;
using WeDonekRpc.Helper;

namespace Basic.OAService.FormInter
{
    internal class FormInterService : IFormInterService
    {
        private readonly IDataFillInCollect _DataFillIn;

        public FormInterService ( IDataFillInCollect dataFillIn )
        {
            this._DataFillIn = dataFillIn;
        }
        public PhyFormColValue[] GetValues ( PhyValuesGetArg arg )
        {
            if ( arg.PhyTableName == "EmpDataFillIn" )
            {
                var data = this._DataFillIn.Get(arg.LinkPk, a => new
                {
                    a.BeginDate,
                    a.EndDate,
                    a.FillType
                });
                return arg.Cols.ConvertAll(a =>
                {
                    PhyFormColValue val = new PhyFormColValue
                    {
                        IsNull = false,
                        Key = a.Key
                    };
                    if ( a.ColName == "FillType" )
                    {
                        val.Value = data.FillType;
                    }
                    else if ( a.ColName == "BeginDate")
                    {
                        val.Value = data.BeginDate;
                    }
                    else if ( a.ColName == "EndDate" )
                    {
                        val.Value = data.EndDate;
                    }
                    else
                    {
                        val.IsNull = true;
                    }
                    return val;
                });
            }
            throw new ErrorException("oa.form.table.name.error");
        }
        public PhyFormValue GetValue ( PhyValueGetArg arg )
        {
            if ( arg.PhyTableName == "EmpDataFillIn" )
            {
                var data = this._DataFillIn.Get(arg.LinkPk, a => new
                {
                    a.BeginDate,
                    a.EndDate,
                    a.FillType
                });
                if ( arg.ColName == "FillType" )
                {
                    return new PhyFormValue
                    {
                        IsNull=false,
                        Value = data.FillType
                    };
                }
                else if ( arg.ColName == "BeginDate" )
                {
                    return new PhyFormValue
                    {
                        IsNull = false,
                        Value = data.BeginDate
                    };
                }
                else if ( arg.ColName == "EndDate" )
                {
                    return new PhyFormValue
                    {
                        IsNull = false,
                        Value = data.EndDate
                    };
                }
                else
                {
                    return new PhyFormValue
                    {
                        IsNull = true
                    };
                }
            }
            throw new ErrorException("oa.form.table.name.error");
        }
        public PhyFormTable[] GetTables ( string name )
        {
            if ( name == "EmpDataFillIn" )
            {
                PhyFormTable table = new PhyFormTable
                {
                    TableLabel = "信息填报表",
                    TableName = name,
                    Column = new PhyFormColumn[]
                    {
                        new PhyFormColumn
                        {
                            Name="FillType",
                            Label="填报类型",
                            ColumnSet = new  PhyColumnSet
                            {
                                DicId=9
                            },
                            DataType = FormRecordDataType.String,
                            FuncType=  FormColumnFuncType.无
                        },
                        new PhyFormColumn
                        {
                            Name="BeginDate",
                            Label="开始日期",
                            DataType = FormRecordDataType.Date,
                            FuncType= FormColumnFuncType.无
                        },
                        new PhyFormColumn
                        {
                            Name="EndDate",
                            Label="结束日期",
                            DataType = FormRecordDataType.Date,
                            FuncType= FormColumnFuncType.无
                        }
                    }
                };
                return new PhyFormTable[]
                {
                    table
                };
            }
            throw new ErrorException("oa.form.table.name.error");
        }
    }
}
